package com.extendRelation.service;

import com.extendRelation.model2.Company;
import com.extendRelation.model2.Employee;
import com.extendRelation.model2.HourlyEmployee;
import com.extendRelation.model2.SalariedEmployee;
import com.utils.HibernateUtil;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class BusinessService2 {

    private SessionFactory sessionFactory = new HibernateUtil().getSessionFactory();

    public void saveEmployee(Employee employee) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        List results = new ArrayList();
        try {
            tx = session.beginTransaction();
            session.save(employee);
            tx.commit();
        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            throw e;
        } finally {
            session.close();
        }
    }

    public List findAllHourlyEmployees() {
        Session session = sessionFactory.openSession();
        Transaction tx = null;

        try {

            tx = session.beginTransaction();
            List results = session.createQuery("from HourlyEmployee").list();
            tx.commit();
            return results;
        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            throw e;
        } finally {
            session.close();
        }
    }

    public List findAllEmployees() {
        Session session = sessionFactory.openSession();
        Transaction tx = null;

        try {
            tx = session.beginTransaction();
            List results = session.createQuery("from Employee").list();
            tx.commit();
            return results;
        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            throw e;
        } finally {
            session.close();
        }
    }

    public Company loadCompany(long id) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Company company = (Company) session.get(Company.class, new Long(id));
            Hibernate.initialize(company.getEmployees());
            tx.commit();
            return company;
        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            throw e;
        } finally {
            session.close();
        }
    }

    public void test() {
        List hourlyEmployees = findAllHourlyEmployees();
        printAllEmployees(hourlyEmployees.iterator());

        List employees = findAllEmployees();
        printAllEmployees(employees.iterator());

        Company company = loadCompany(1);
        printAllEmployees(company.getEmployees().iterator());

        Employee employee = new HourlyEmployee("Mary", 300, company);
        saveEmployee(employee);
        sessionFactory.close();

    }

    private void printAllEmployees(Iterator it) {
        while (it.hasNext()) {
            Employee e = (Employee) it.next();
            if (e instanceof HourlyEmployee) {
                System.out.println(((HourlyEmployee) e).getRate());
            } else
                System.out.println(((SalariedEmployee) e).getSalary());
        }
    }
    public static void main(String args[]) {
        new BusinessService2().test();
    }
}
